]> dgit.raspbian.org Git - gnome-shell.git/commitdiff
shell: Add helpers to start/stop systemd units
authorCarlos Garnacho <carlosg@gnome.org>
Thu, 15 Aug 2019 11:22:24 +0000 (13:22 +0200)
committerCarlos Garnacho <mrgarnacho@gmail.com>
Thu, 5 Sep 2019 07:42:46 +0000 (07:42 +0000)
These just send the respective DBus message to the systemd user service.

https://gitlab.gnome.org/GNOME/gnome-shell/merge_requests/680

src/shell-util.c
src/shell-util.h

index 01be73b0b0501a3eedf784a03fcc25e3b6a44b4b..d38c5ab98e50e9ea7f385db9f5032c66eec13354 100644 (file)
@@ -609,3 +609,62 @@ shell_util_check_cloexec_fds (void)
   fdwalk (check_cloexec, NULL);
   g_info ("Open fd CLOEXEC check complete");
 }
+
+static void
+on_systemd_call_cb (GObject      *source,
+                    GAsyncResult *res,
+                    gpointer      user_data)
+{
+  g_autoptr (GVariant) reply = NULL;
+  g_autoptr (GError) error = NULL;
+  const gchar *command = user_data;
+
+  reply = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source),
+                                         res, &error);
+  if (error)
+    g_warning ("Could not issue '%s' systemd call", command);
+}
+
+static gboolean
+shell_util_systemd_call (const char  *command,
+                         const char  *unit,
+                         const char  *mode,
+                         GError     **error)
+{
+  g_autoptr (GDBusConnection) connection = NULL;
+
+  connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, error);
+
+  if (connection == NULL)
+    return FALSE;
+
+  g_dbus_connection_call (connection,
+                          "org.freedesktop.systemd1",
+                          "/org/freedesktop/systemd1",
+                          "org.freedesktop.systemd1.Manager",
+                          command,
+                          g_variant_new ("(ss)",
+                                         unit, mode),
+                          NULL,
+                          G_DBUS_CALL_FLAGS_NONE,
+                          -1, NULL,
+                          on_systemd_call_cb,
+                          (gpointer) command);
+  return TRUE;
+}
+
+gboolean
+shell_util_start_systemd_unit (const char  *unit,
+                               const char  *mode,
+                               GError     **error)
+{
+  return shell_util_systemd_call ("StartUnit", unit, mode, error);
+}
+
+gboolean
+shell_util_stop_systemd_unit (const char  *unit,
+                              const char  *mode,
+                              GError     **error)
+{
+  return shell_util_systemd_call ("StopUnit", unit, mode, error);
+}
index 6904f43bc3157a0073ef1eb123205d5258ef0f06..c41e02c79888fef6419bf8e520b957aa7087add0 100644 (file)
@@ -59,6 +59,13 @@ cairo_surface_t * shell_util_composite_capture_images (ClutterCapture  *captures
 
 void shell_util_check_cloexec_fds (void);
 
+gboolean shell_util_start_systemd_unit (const char  *unit,
+                                        const char  *mode,
+                                        GError     **error);
+gboolean shell_util_stop_systemd_unit  (const char  *unit,
+                                        const char  *mode,
+                                        GError     **error);
+
 G_END_DECLS
 
 #endif /* __SHELL_UTIL_H__ */